package s_002_Bean_Life_Cycle.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * 指定初始化和销毁方法：bean的后置处理器BeanPostProcessor【interface】，在bean初始化前后进行一些处理工作；
 * BeanPostProcessor.postProcessBeforeInitialization:在初始化之前工作
 * BeanPostProcessor.postProcessAfterInitialization:在初始化之后工作
 * ====================================================================================================
 * BeanPostProcessor工作流程；
 * 遍历得到容器中所有的BeanPostProcessor，挨个执行beforeInitialization，
 * 一但返回null，跳出for循环，不会执行后面的BeanPostProcessor.postProcessorsBeforeInitialization
 *
 * populateBean(beanName, mbd, instanceWrapper);给bean进行属性赋值
 * initializeBean {
 *      applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
 *      invokeInitMethods(beanName, wrappedBean, mbd);执行自定义初始化
 *      applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
 * }
 * ====================================================================================================
 */@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization..." + beanName + "=>" + bean);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization..." + beanName + "=>" + bean);
        return bean;
    }
}